﻿using GKQC.Common;
using Microsoft.SqlServer.Server;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Ports;
using System.Threading;

namespace GKQC.Machine
{
    /// <summary>
    /// CH-2311 LC仪器
    /// </summary>
    public class LcMeter2311: ILcMeter
    {
        public string Prefix { get; set; }
        public bool Check(SerialPort sp)
        {
            bool flag = false;

            byte[] SendBytes = null;
            string SendData = "*RMT" + Environment.NewLine;
            SendBytes = System.Text.Encoding.Default.GetBytes(SendData);
            sp.Write(SendBytes, 0, SendBytes.Length);   //发送数据
            Thread.Sleep(50);
            string result = GetData(sp);
            if (!result.Contains("0,OK"))
            {
                Thread.Sleep(200);
                sp.Write(SendBytes, 0, SendBytes.Length);   //发送数据

                result = GetData(sp);
                if (!result.Contains("0,OK"))
                {
                    Thread.Sleep(250);
                    sp.Write(SendBytes, 0, SendBytes.Length);   //发送数据
                }
                else
                    flag = true;
            }
            else
                flag = true;

            return flag;
        }

        public void ControlMachine(SerialPort sp)
        {
            byte[] SendBytes = null;
            string SendData = "*RMT" + Environment.NewLine;
            SendBytes = System.Text.Encoding.Default.GetBytes(SendData);
            sp.Write(SendBytes, 0, SendBytes.Length);//发送数据
            Thread.Sleep(50);
            string result = GetData(sp);
            if (!result.Contains("0,OK"))
            {
                Thread.Sleep(200);
                sp.Write(SendBytes, 0, SendBytes.Length);//发送数据 
                result = GetData(sp);
                if (!result.Contains("0,OK"))
                {
                    Thread.Sleep(250);
                    sp.Write(SendBytes, 0, SendBytes.Length);//发送数据
                }
            }
        }

        public void UnControlMachine(SerialPort sp)
        {
            byte[] SendBytes = null;
            string SendData = "*END" + Environment.NewLine;
            SendBytes = System.Text.Encoding.Default.GetBytes(SendData);
            sp.Write(SendBytes, 0, SendBytes.Length);//发送数据
            Thread.Sleep(50);
            string result = GetData(sp);
            if (!result.Contains("0,OK"))
            {
                Thread.Sleep(200);
                sp.Write(SendBytes, 0, SendBytes.Length);//发送数据 
                result = GetData(sp);
                if (!result.Contains("0,OK"))
                {
                    Thread.Sleep(250);
                    sp.Write(SendBytes, 0, SendBytes.Length);//发送数据
                }
            }
        }

        public void SetLeak(SerialPort sp, string leak)
        {

            string SendData = "PROG:LEAK" + leak + Environment.NewLine;
            byte[] SendBytes = System.Text.Encoding.Default.GetBytes(SendData);
            Thread.Sleep(200);
            sp.Write(SendBytes, 0, SendBytes.Length);//发送数据
            Thread.Sleep(50);                               //Thread.Sleep(50);
            string result = GetData(sp);
            if (!result.Contains("0,OK"))
            {
                Thread.Sleep(200);
                sp.Write(SendBytes, 0, SendBytes.Length);//发送数据 
                result = GetData(sp);
                if (!result.Contains("0,OK"))
                {
                    Thread.Sleep(250);
                    sp.Write(SendBytes, 0, SendBytes.Length);//发送数据
                }
            }
        }

        public void SetVoltage(SerialPort sp, decimal voltage)
        {
            string SendData = "PROG:VOLT" + voltage.ToString() + Environment.NewLine;
            byte[] SendBytes = System.Text.Encoding.Default.GetBytes(SendData);
            Thread.Sleep(200);
            sp.Write(SendBytes, 0, SendBytes.Length);//发送数据
            Thread.Sleep(50);
            string result = GetData(sp);
            if (!result.Contains("0,OK"))
            {
                sp.Write(SendBytes, 0, SendBytes.Length);//发送数据
                result = GetData(sp);
                Thread.Sleep(250);
                if (!result.Contains("0,OK"))
                {
                    Thread.Sleep(300);
                    sp.Write(SendBytes, 0, SendBytes.Length);//发送数据
                }
            }
        }

        public void SetCharTime(SerialPort sp, decimal chargeTime)
        {
            string SendData = "PROG:CHAR" + chargeTime.ToString() + Environment.NewLine;
            byte[] SendBytes = System.Text.Encoding.Default.GetBytes(SendData);
            Thread.Sleep(200);
            sp.Write(SendBytes, 0, SendBytes.Length);//发送数据
            Thread.Sleep(50);
            string result = GetData(sp);
            if (!result.Contains("0,OK"))
            {
                Thread.Sleep(200);
                sp.Write(SendBytes, 0, SendBytes.Length);//发送数据
                result = GetData(sp);
                if (!result.Contains("0,OK"))
                {
                    Thread.Sleep(200);
                    sp.Write(SendBytes, 0, SendBytes.Length);//发送数据
                    result = GetData(sp);
                    if (!result.Contains("0,OK"))
                    {
                        Thread.Sleep(250);
                        sp.Write(SendBytes, 0, SendBytes.Length);//发送数据
                    }
                }
            }
        }

        public void SetDisChargeTime(SerialPort sp, decimal disChargeTime)
        {
            string SendData = "PROG:DISC" + disChargeTime.ToString() + Environment.NewLine;
            byte[] SendBytes = System.Text.Encoding.Default.GetBytes(SendData);
            Thread.Sleep(200);
            sp.Write(SendBytes, 0, SendBytes.Length);//发送数据
            Thread.Sleep(50);
            string result = GetData(sp);
            if (!result.Contains("0,OK"))
            {
                Thread.Sleep(200);
                sp.Write(SendBytes, 0, SendBytes.Length);//发送数据
                result = GetData(sp);
                if (!result.Contains("0,OK"))
                {
                    Thread.Sleep(250);
                    sp.Write(SendBytes, 0, SendBytes.Length);//发送数据
                }
            }
        }

        void ILcMeter.SetTestTime(SerialPort sp, decimal testTime)
        {
            string SendData = "PROG:TEST" + testTime.ToString() + Environment.NewLine;
            byte[] SendBytes = System.Text.Encoding.Default.GetBytes(SendData);
            Thread.Sleep(200);
            sp.Write(SendBytes, 0, SendBytes.Length);//发送数据
            string result = GetData(sp);
            if (!result.Contains("0,OK"))
            {
                Thread.Sleep(200);
                sp.Write(SendBytes, 0, SendBytes.Length);//发送数据
                result = GetData(sp);
                if (!result.Contains("0,OK"))
                {
                    Thread.Sleep(200);
                    sp.Write(SendBytes, 0, SendBytes.Length);//发送数据
                    result = GetData(sp);
                    if (!result.Contains("0,OK"))
                    {
                        Thread.Sleep(200);
                        sp.Write(SendBytes, 0, SendBytes.Length);//发送数据
                    }
                }
            }
        }

        public void Char(SerialPort sp)
        {
            byte[] SendBytes = null;
            Thread.Sleep(200);
            string SendData = "CHAR" + Environment.NewLine;
            SendBytes = System.Text.Encoding.Default.GetBytes(SendData);
            sp.Write(SendBytes, 0, SendBytes.Length);   //发送数据
            Thread.Sleep(50);
            string result = GetData(sp);
            if (!result.Contains("0,OK"))
            {
                Thread.Sleep(200);
                sp.Write(SendBytes, 0, SendBytes.Length);   //发送数据
                result = GetData(sp);
                if (!result.Contains("0,OK"))
                {
                    Thread.Sleep(250);
                    sp.Write(SendBytes, 0, SendBytes.Length);   //发送数据
                }
            }
        }

        public void DisCharge(SerialPort sp)
        {
            byte[] SendBytes = null;
            string SendData = "DISC" + Environment.NewLine;
            SendBytes = System.Text.Encoding.Default.GetBytes(SendData);
            sp.Write(SendBytes, 0, SendBytes.Length);   //发送数据
            Thread.Sleep(50);
            string result = GetData(sp);
            if (!result.Contains("0,OK"))
            {
                Thread.Sleep(200);
                sp.Write(SendBytes, 0, SendBytes.Length);   //发送数据

                result = GetData(sp);
                //sp.DiscardInBuffer();
                if (!result.Contains("0,OK"))
                {
                    Thread.Sleep(250);
                    sp.Write(SendBytes, 0, SendBytes.Length);   //发送数据
                }
            }
        }

        public void Test(SerialPort sp)
        {
            byte[] SendBytes = null;
            string SendData = "TEST" + Environment.NewLine;
            SendBytes = System.Text.Encoding.Default.GetBytes(SendData);
            sp.Write(SendBytes, 0, SendBytes.Length);   //发送数据
            Thread.Sleep(50);
            string result = GetData(sp);
            if (!result.Contains("0,OK"))
            {
                Thread.Sleep(200);
                sp.Write(SendBytes, 0, SendBytes.Length);   //发送数据

                result = GetData(sp);
                if (!result.Contains("0,OK"))
                {
                    Thread.Sleep(250);
                    sp.Write(SendBytes, 0, SendBytes.Length);   //发送数据
                }
            }
        }

        public int GetChargeTime(SerialPort sp)
        {
            int time = 60;
            byte[] SendBytes = null;
            string SendData = "PROG:CHAR?" + Environment.NewLine;
            SendBytes = System.Text.Encoding.Default.GetBytes(SendData);
            sp.Write(SendBytes, 0, SendBytes.Length);   //发送数据
            Thread.Sleep(200);
            string result = GetData(sp).Trim().Replace("\r\n", string.Empty);
            if (!int.TryParse(result.Trim(), out time))
            {
                //Logger.LogMessage("獲取充電時間錯誤：" + result);
                sp.Write(SendBytes, 0, SendBytes.Length);   //发送数据
                Thread.Sleep(200);
                result = GetData(sp).Trim().Replace("\r\n", string.Empty);
                if (!int.TryParse(result.Trim(), out time))
                {
                    //Logger.LogMessage("獲取充電時間錯誤：" + result);
                    
                    sp.Write(SendBytes, 0, SendBytes.Length);   //发送数据
                    Thread.Sleep(250);
                    result = GetData(sp).Trim().Replace("\r\n", string.Empty);
                    if (!int.TryParse(result.Trim(), out time))
                    {
                        //Logger.LogMessage("獲取充電時間錯誤：" + result);
                        sp.Write(SendBytes, 0, SendBytes.Length);   //发送数据
                    }
                }
            }

            //if (time > 10) time = time - 2;
            return time;
            //Logger.LogMessage("充電時間：" + result);
        }

        public int GetTestTime(SerialPort sp)
        {
            int time = 0;
            byte[] SendBytes = null;
            string SendData = "PROG:TEST?" + Environment.NewLine;
            SendBytes = System.Text.Encoding.Default.GetBytes(SendData);
            sp.Write(SendBytes, 0, SendBytes.Length);   //发送数据
            Thread.Sleep(50);
            string result = GetData(sp).Trim().Replace("\r\n", string.Empty);
            if (!int.TryParse(result.Trim(), out time))
            {
                Thread.Sleep(200);
                sp.Write(SendBytes, 0, SendBytes.Length);   //发送数据
                result = GetData(sp).Trim().Replace("\r\n", string.Empty);
                if (!int.TryParse(result.Trim(), out time))
                {
                    Thread.Sleep(200);
                    sp.Write(SendBytes, 0, SendBytes.Length);   //发送数据
                    result = GetData(sp).Trim().Replace("\r\n", string.Empty);
                    if (!int.TryParse(result.Trim(), out time))
                    {
                        Thread.Sleep(200);
                        sp.Write(SendBytes, 0, SendBytes.Length);   //发送数据
                    }
                }
            }
            return time;
        }

        public decimal GetVoltage(SerialPort sp)
        {
            int voltage = 0;
            byte[] SendBytes = null;
            string SendData = "PROG:VOLT?" + Environment.NewLine;
            SendBytes = System.Text.Encoding.Default.GetBytes(SendData);
            sp.Write(SendBytes, 0, SendBytes.Length);   //发送数据
            Thread.Sleep(50);
            string result = GetData(sp).Trim().Replace("\r\n", string.Empty);
            //50.0 50.0 50.0
            if (!int.TryParse(result.Trim(), out voltage))
            {
                Thread.Sleep(200);
                sp.Write(SendBytes, 0, SendBytes.Length);   //发送数据
                result = GetData(sp).Trim().Replace("\r\n", string.Empty);
                if (!int.TryParse(result.Trim(), out voltage))
                {
                    Thread.Sleep(200);
                    sp.Write(SendBytes, 0, SendBytes.Length);   //发送数据
                    result = GetData(sp).Trim().Replace("\r\n", string.Empty);
                    if (!int.TryParse(result.Trim(), out voltage))
                    {
                        Thread.Sleep(250);
                        sp.Write(SendBytes, 0, SendBytes.Length);   //发送数据
                    }
                }
            }
            return voltage;
        }

        public string GetData(SerialPort sp)
        {
            try
            {
                byte[] readBuffer = new byte[sp.BytesToRead];
                sp.Read(readBuffer, 0, readBuffer.Length);
                return System.Text.Encoding.ASCII.GetString(readBuffer);
            }
            catch (IOException IOEx)
            {
                Logger.LogError(IOEx);
            }
            catch (InvalidOperationException IVOEx)
            {
                Logger.LogError(IVOEx);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
            }
            return "";
        }

        public List<double> AnalysisData(string rawData)
        {
            List<double> list = new List<double>();

            if (!string.IsNullOrWhiteSpace(rawData))
            {
                //16.0V, 0.245uA
                //16.0V, 0.245uA
                string[] trings1 = rawData.Split(new string[] { "\r\n" }, StringSplitOptions.None);
                if (trings1 == null || trings1.Length == 0) return list;

                //16.0V, 0.245uA
                //16.0V, 0.245uA
                int rightIndex = -1;
                for (int i = trings1.Length - 1; i > -1; i--)
                {
                    if (trings1[i].Contains("V") && trings1[i].Contains("A"))
                    {
                        rightIndex = i;
                        break;
                    }
                }

                if (rightIndex == -1) return list;
                string[] strings = trings1[rightIndex].Split(',');

                if (strings == null || strings.Length < 2) return list;

                double voltage = 0;
                if (strings[0].Contains("V"))
                { 
                    double.TryParse(strings[0].Replace("V",string.Empty), out voltage);
                }
                list.Add(voltage);

                if (strings[1].Contains("A"))
                {
                    double times = 1;
                    if (strings[1].Contains("mA"))
                        times = 1000;
                    else if (strings[1].Contains("uA"))
                        times = 1;
                    else return list;

                    string s = strings[1].Trim().Replace("mA", string.Empty).Replace("uA", string.Empty);
                    double value = 0;
                    if (double.TryParse(s, out value))
                    {
                        value = value * times;
                        value = Math.Round(value, 2);
                        //if (value > 0m)
                        {
                            list.Add(value);
                        }
                    }
                }
            }

            return list;
        }

        public string GetLeak(SerialPort sp, string add)
        {
            return string.Empty;
        }
    }
}
